package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.good.Goods;
import com.cskaoyan.mapper.*;
import com.cskaoyan.weixin.bean.goods.AttributeBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.lang.System;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    GoodsAttributeMapper goodsAttributeMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;
    @Autowired
    GoodsSpecificationMapper goodsSpecificationMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    BrandMapper brandMapper;

    /**
     * 显示全部商品信息
     *
     * @param page
     * @return
     */
    @Override
    public GoodsVo<Goods> queryGoodsList(Integer page, Integer limit, String sort, String order, Integer goodsSn, String name) {
        PageHelper.startPage(page, limit);
        GoodsVo<Goods> goodsvo = new GoodsVo<>();
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        if (goodsSn != null) {
            criteria.andIdEqualTo(goodsSn);
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameEqualTo(name);
        }
        List<Goods> goodsList = goodsMapper.selectByExample(goodsExample);
        PageInfo pageInfo = new PageInfo(goodsList);
        long total = pageInfo.getTotal();
        goodsvo.setItems(goodsList);
        goodsvo.setTotal(total);
        return goodsvo;
    }

    /**
     * 商品详情
     *
     * @param id
     * @return
     */
    @Override
    public GoodsListVO queryGoodsDetail(Integer id) {
        GoodsListVO goodsListVO = new GoodsListVO();
        Goods goods = goodsMapper.selectByPrimaryKey(id);

        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();

        GoodsAttributeExample goodsAttributeExample = new GoodsAttributeExample();
        GoodsAttributeExample.Criteria criteria1 = goodsAttributeExample.createCriteria();

        GoodsProductExample goodsProductExample = new GoodsProductExample();
        GoodsProductExample.Criteria criteria2 = goodsProductExample.createCriteria();

        GoodsSpecificationExample goodsSpecificationExample = new GoodsSpecificationExample();
        GoodsSpecificationExample.Criteria criteria3 = goodsSpecificationExample.createCriteria();

        List<Integer> categoryIds = new ArrayList<>();
        Category category = categoryMapper.selectByPrimaryKey(goods.getCategoryId());
        if (category.getPid() != 0) {
            categoryIds.add(category.getPid());
        }
        categoryIds.add(category.getId());
        goodsListVO.setCategoryIds(categoryIds);

        if (id != null) {
            criteria1.andGoodsIdEqualTo(id);
            criteria2.andGoodsIdEqualTo(id);
            criteria3.andGoodsIdEqualTo(id);
        }
        List<GoodsAttribute> goodsAttributeList = goodsAttributeMapper.selectByExample(goodsAttributeExample);
        List<GoodsProduct> goodsProductList = goodsProductMapper.selectByExample(goodsProductExample);
        List<GoodsSpecification> goodsSpecificationList = goodsSpecificationMapper.selectByExample(goodsSpecificationExample);
        goodsListVO.setGoods(goods);
        goodsListVO.setProducts(goodsProductList);
        goodsListVO.setAttributes(goodsAttributeList);
        goodsListVO.setSpecifications(goodsSpecificationList);
        return goodsListVO;
    }

    /**
     * 商品品牌信息（编辑时会用到）
     *
     * @return
     */
    @Override
    public CatAndBrandVO goodsCatAndBrand() {
        List<Cat> catList = categoryMapper.selectCatList();
        List<ValueAndLabel> valueAndLabels = brandMapper.selectBraList();
        CatAndBrandVO catAndBrandVO = new CatAndBrandVO();
        catAndBrandVO.setCategoryList(catList);
        catAndBrandVO.setBrandList(valueAndLabels);
        return catAndBrandVO;
    }


    //添加商品
    @Override
    @Transactional
    public void createGoods(GoodsBO goods) {
        Goods good = goods.getGoods();
        good.setAddTime(new Date());
        good.setUpdateTime(new Date());
        goodsMapper.insertSelective(good);
        int goodId = goodsMapper.selectNewId();

        List<GoodsAttribute> attributes = goods.getAttributes();
        for (GoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goodId);
            attribute.setAddTime(new Date());
            goodsAttributeMapper.insertSelective(attribute);
        }
        List<GoodsProduct> products = goods.getProducts();
        for (GoodsProduct product : products) {
            product.setGoodsId(goodId);
            product.setAddTime(new Date());
            product.setId(null);
            goodsProductMapper.insert(product);
        }
        for (GoodsSpecification specification : goods.getSpecifications()) {
            specification.setGoodsId(goodId);
            specification.setAddTime(new Date());
            goodsSpecificationMapper.insertSelective(specification);
        }
    }

    /**
     * 更新商品信息
     * 1，goods表
     * 2，products表
     * 3，attributes表
     * 4，specification表
     *
     * @param goodsBO
     * @return
     */
    @Override
    @Transactional
    public void updateGoods(GoodsBO goodsBO) {
        //goods表
        Date date = new Date();
        Goods goods = goodsBO.getGoods();
        goods.setUpdateTime(date);
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andIdEqualTo(goods.getId());
        goodsMapper.updateByExample(goods,goodsExample);
        Integer goodId = goods.getId();

        //更新   attributes表
        List<GoodsAttribute> attributes = goodsBO.getAttributes();
        if (attributes.size() == 0) {
            GoodsAttributeExample goodsAttributeExample = new GoodsAttributeExample();
            GoodsAttributeExample.Criteria criteria1 = goodsAttributeExample.createCriteria();
            criteria1.andGoodsIdEqualTo(goodId);
            goodsAttributeMapper.deleteByExample(goodsAttributeExample);
        } else {
            for (GoodsAttribute attributesBean : attributes) {
                if (attributesBean.getGoodsId() == null) {
                    attributesBean.setGoodsId(goodId);
                    attributesBean.setAddTime(new Date());
                    goodsAttributeMapper.insertSelective(attributesBean);
                } else {
                    goodsAttributeMapper.deleteByPrimaryKey(attributesBean.getId());
                    attributesBean.setUpdateTime(date);
                    goodsAttributeMapper.insertSelective(attributesBean);
                }
            }
        }

        //更新specification表
        List<GoodsSpecification> specificationsBeanList = goodsBO.getSpecifications();
        if (specificationsBeanList.size() == 0) {
            GoodsSpecificationExample goodsSpecificationExample = new GoodsSpecificationExample();
            GoodsSpecificationExample.Criteria criteria1 = goodsSpecificationExample.createCriteria();
            criteria1.andGoodsIdEqualTo(goodId);
            goodsSpecificationMapper.deleteByExample(goodsSpecificationExample);
        } else {
            for (GoodsSpecification specificationsBean : specificationsBeanList) {
                if (specificationsBean.getGoodsId() == null) {
                    specificationsBean.setGoodsId(goodId);
                    specificationsBean.setAddTime(new Date());
                    goodsSpecificationMapper.insertSelective(specificationsBean);
                } else {
                    goodsSpecificationMapper.deleteByPrimaryKey(specificationsBean.getId());
                    specificationsBean.setUpdateTime(date);
                    goodsSpecificationMapper.insertSelective(specificationsBean);
                }
            }
        }

        //更新products表
        List<GoodsProduct> products = goodsBO.getProducts();
        if (specificationsBeanList.size() == 0) {
            GoodsProductExample goodsProductExample = new GoodsProductExample();
            GoodsProductExample.Criteria criteria1 = goodsProductExample.createCriteria();
            criteria1.andGoodsIdEqualTo(goodId);
            goodsProductMapper.deleteByExample(goodsProductExample);
        } else {
            for (GoodsProduct product : products) {
                if (product.getGoodsId() == null) {
                    product.setGoodsId(goodId);
                    product.setAddTime(new Date());
                    product.setId(null);
                    goodsProductMapper.insertSelective(product);
                } else {
                    goodsProductMapper.deleteByPrimaryKey(product.getId());
                    product.setUpdateTime(date);
                    goodsProductMapper.insertSelective(product);
                }
            }
        }
    }

    /**
     * 删除和goods表相关表的其他表信息
     * @param goods
     */
    @Override
    public void deleteGoods(Goods goods) {
        Integer id=goods.getId();
        goodsMapper.deleteByPrimaryKey(id);
        GoodsProductExample goodsProductExample=new GoodsProductExample();
        goodsProductExample.createCriteria().andGoodsIdEqualTo(id);
        goodsProductMapper.deleteByExample(goodsProductExample);

        GoodsAttributeExample attributeExample=new GoodsAttributeExample();
        attributeExample.createCriteria().andGoodsIdEqualTo(id);
        goodsAttributeMapper.deleteByExample(attributeExample);

        GoodsSpecificationExample goodsSpecificationExample=new GoodsSpecificationExample();
        goodsSpecificationExample.createCriteria().andGoodsIdEqualTo(id);
        goodsSpecificationMapper.deleteByExample(goodsSpecificationExample);
    }
}
